#pragma once
#include <iostream>
#include <unordered_map>
#include <fstream>
#include <vector>

#include "Log.hpp"

using namespace std;
string dic_path_default = "./recource/dict.txt";
const string dep = " ";




class Translate
{
public:
    Translate(string dic_path =  dic_path_default):_dic_path(dic_path)
    {
        LoadDict();
        Parse();
    }

    void LoadDict()
    {
        ifstream in(_dic_path);   
        string line;
        while (getline(in,line))
        {
            lines.push_back(line);
        }
        in.close();
        lg.LogMessage(Debug,"Load Dict success path : %s\n",_dic_path.c_str());
    }

//分析器
    void Parse()
    {
        for(auto line: lines) 
        {
            auto pos = line.find(dep);//返回首字符到该字符的距离
            if(pos == string::npos) continue;
            else{
            string word = line.substr(0,pos);
            string chinese = line.substr(pos+dep.size()); 
            _dict.insert(make_pair(word,chinese));                
            }
        }
            lg.LogMessage(Debug,"Parse Dict success path : %s\n",_dic_path.c_str());

    }

    void debug()
    {
        for(auto elem: _dict) 
        {
            cout << elem.first << ":" << elem.second << endl;
        }
    }
    string Excute(const string &word)
    {
        auto it = _dict.find(word);
        if(it == _dict.end()) return "unknown";        
        else return _dict[word];
    }

    ~Translate()
    {

    }

private:
    string _dic_path;
    unordered_map<string,string> _dict;

    vector<string> lines;

}; 